package com.shouhuobao.take.active;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;

import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.active.adapter.ActiveAdapter;
import com.shouhuobao.take.model.ActiveBean;

import droid.frame.view.xlist.SListView;

/**
 * Created by joker on 2017/9/21.
 * 活动基类fragment
 */
public class BaseActiveFragment extends BaseFragment {

    protected SListView mListView;
    protected ActiveAdapter mAdapter = null;
    protected ReqResult<ActiveBean> result;
    protected SwipeRefreshLayout mRefreshLayout;
    protected int activeType = 0;//0 进行中 1 历史

    @Override
    protected void findViewById() {
        super.findViewById();
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mListView = (SListView) findViewById(R.id.active_listiew);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ActiveBean bean = (ActiveBean) parent.getItemAtPosition(position);
                if (bean != null) {
                    Intent intent = new Intent(getActivityContext(), ActiveActivity.class);
                    intent.putExtra("item", bean);
                    intent.putExtra("activeType",activeType);
                    startActivity(intent);
                }
            }
        });
        // mEmptyLayout = (LinearLayout) findViewById(R.id.empty_receive_layout);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().queryActiveData(activeType);
            }
        });
    }

    protected ReqResult<ActiveBean> handleMessage(Object msgObj) {
        cancelLoadingDialog();
        onRefreshComplete();
        try {
            result = JSON.parseList(msgObj, ActiveBean.class);// 解析可能出现问题
            getActivityContext().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (checkLoginStatus(result)) {
                        if (mListView.getAdapter() == null) {
                            mListView.setAdapter(mAdapter);
                        }
                        mAdapter.setItems(result.getResultList(), true);
                    } else {
                        // 未知原因是否需要显示
                        showToast(result.getMessage());
                    }
                }
            });
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
